﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Models.Extensions
{
    /// <summary>
    /// Extension methods to classes used in the SQL provider
    /// </summary>
    public static class ServerExtensions
    {
        /// <summary>
        /// Return a string value from a Dictionary<string, string> item
        /// </summary>
        /// <param name="item">The dictionary item to get the value from</param>
        /// <param name="keyName">The key name to text for</param>
        /// <param name="defaultValue">The default value to use if the key name does not exist</param>
        /// <returns>If the key name exists then this will be returned, otherwise the defaultValue will be returned</returns>
        public static string GetString(this Dictionary<string, string> item, string keyName, string defaultValue)
        {
            string result = defaultValue;

            if (item.Keys.Contains(keyName))
            {
                result = item[keyName] ?? defaultValue;
            }

            return result;
        }

        /// <summary>
        /// Return a boolan value from a Dictionary<string, string> item
        /// </summary>
        /// <param name="item">The dictionary item to get the value from</param>
        /// <param name="keyName">The key name to text for</param>
        /// <param name="defaultValue">The default value to use if the key name does not exist</param>
        /// <returns>If the key name exists then this will be returned, otherwise the defaultValue will be returned</returns>
        public static bool GetBool(this Dictionary<string, string> item, string keyName, bool defaultValue)
        {

            bool result = defaultValue;

            if (item.Keys.Contains(keyName))
            {
                result = (item[keyName] == bool.TrueString);
            }

            return result;
        }
    }
}
